<?php
/**
 * Created by PhpStorm.
 * User: wuchao
 * Date: 2020/4/26
 * Time: 15:11
 */

namespace app\controller;

use app\BaseController;
use app\model\BoxModel;
use think\facade\Db;
use think\Request;
use app\middleware\Check;
use think\facade\Validate;
use think\response\Json;
use think\validate\ValidateRule;

/**
 * Class SchoolRole 前端 箱子控制器
 * @package app\controller
 */
class Box extends BaseController
{
    protected $middleware = [Check::class];

    /**查看箱子使用状态
     * @param Request $request
     * @return Json
     */
    public function boxStatus(Request $request)
    {
        $input['code'] = $request->post('code');//箱子二维码值
        $input['school_id'] = $request->post('school_id');//箱子二维码值
        // 参数校验
        $validate = Validate::rule([
            'code' => ValidateRule::isRequire(null, '缺少参数'),
            'school_id' => ValidateRule::isRequire(null, '缺少参数'),
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        //逻辑处理
        return BoxModel::boxStatus($input);
    }

    /**箱子修改状态
     * @param Request $request
     * @return Json
     */
    public function boxUse(Request $request)
    {
        $input['code'] = $request->post('code');//箱子二维码值
        $input['type'] = $request->post('type');//1:楼栋管理员收货 2：大配送提货3.整箱撤回
        // 参数校验
        $validate = Validate::rule([
            'code' => ValidateRule::isRequire(null, '缺少参数'),
            'type' => function ($value) {
                return in_array($value,['1','2','3'], true) ? true : '不合法参数' ;
            },
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        //逻辑处理
        return BoxModel::boxUse($input);
    }

    /**修改订单值{装箱员、箱子编号}
     * @param Request $request
     * @return Json
     */
    public function boxModify(Request $request)
    {
        $input['data'] = $request->post('data');//组合数据
        $input['uid'] = $request->post('uid');//装箱员ID
        $input['type'] = $request->post('type');//1:检测 2：装入
        // 参数校验
        $validate = Validate::rule([
            'data' => ValidateRule::isRequire(null, '缺少参数'),
            'uid' => ValidateRule::isRequire(null, '缺少参数'),
            'type' => function($v) {
                return in_array($v, ['1','2'], true) ? true : '不合法参数' ;
            }
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        //逻辑处理
        return BoxModel::boxModify($input);
    }

    /**装箱员查看订单
     * @param Request $request
     * @return Json
     */
    public function boxOrderList(Request $request)
    {
        $input['uid'] = $request->post('uid');//箱子二维码值
        // 参数校验
        $validate = Validate::rule([
            'uid' => ValidateRule::isRequire(null, '缺少参数'),
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        //逻辑处理
        return BoxModel::boxOrderList($input);
    }

    /**清楚箱子信息
     * @param Request $request
     * @return Json
     */
    public function boxDel(Request $request)
    {
        $input['code'] = $request->post('code');//箱子值
        // 参数校验
        $validate = Validate::rule([
            'code' => ValidateRule::isRequire(null, '缺少参数'),
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        //逻辑处理
        return BoxModel::boxDel($input);
    }

    /**清楚订单装箱信息
     * @param Request $request
     * @return Json
     */
    public function boxOrderDel(Request $request)
    {
        $input['order_no'] = $request->post('order_no');//订单号
        // 参数校验
        $validate = Validate::rule([
            'order_no' => ValidateRule::isRequire(null, '缺少参数'),
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        //逻辑处理
        return BoxModel::boxOrderDel($input);
    }

    /**扫码获得骑手姓名
     * @param Request $request
     * @return Json
     */
    public function getName(Request $request)
    {
        $input['id'] = $request->post('id');//骑手ID
        // 参数校验
        $validate = Validate::rule([
            'id' => ValidateRule::isRequire(null, '缺少参数'),
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        $user = Db::table('eb_user')->where('id', $input['id'])->field('username,is_system_order_taking')->find();
        if ($user['is_system_order_taking'] == 1) return jsonSuccess('获取成功', $user['username']);
        if ($user['is_system_order_taking'] == 0) return jsonFail('该骑手已被禁用，暂不能接单！', $user['username']);
    }

    /**楼栋管理员处----捆绑订单
     * @param Request $request
     * @return Json
     */
    public function bindingOrder(Request $request)
    {
        $input['order_no'] = $request->post('order_no');//订单号
        $input['send_id'] = $request->post('send_id');//骑手ID
        $input['manag_id'] = $request->post('manag_id');//楼栋管理员ID
        // 参数校验
        $validate = Validate::rule([
            'order_no' => ValidateRule::isRequire(null, '缺少参数'),
            'send_id' => ValidateRule::isRequire(null, '缺少参数'),
            'manag_id' => ValidateRule::isRequire(null, '缺少参数'),
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        //逻辑处理
        return BoxModel::bindingOrder($input);
    }

    /**运营角色查看订单数量
     * @param Request $request
     * @return Json
     */
    public function OrderNum(Request $request)
    {
        $input['start_time'] = $request->param('start_time');//开始时间
        $input['end_time'] = $request->param('end_time');//结束时间
        $input['type'] = $request->param('type');//1:楼栋管理员 2：装箱员
        $input['id'] = $request->param('id');//管理员ID
        $input['page'] = $request->param('page', 1);//分页
        $input['limit'] = $request->param('limit', 10);//条数
        // 参数校验
        $validate = Validate::rule([
            'start_time' => ValidateRule::isRequire(null, '缺少参数'),
            'end_time' => ValidateRule::isRequire(null, '缺少参数'),
            'id' => ValidateRule::isRequire(null, '缺少参数'),
            'type' => function ($value, $all) {
                switch ($value) {
                    case 1:
                        return true;
                    case 2:
                        return true;
                    default:
                        return '参数类型错误';
                }
            },
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        //逻辑处理
        return BoxModel::OrderNum($input);
    }

    /**骑手扫码获得单
     * @param Request $request
     * @return Json
     */
    public function OrderScan(Request $request)
    {
        $input['order_no'] = $request->post('order_no');//订单号
        $input['send_id'] = $request->post('send_id');//骑手ID
        // 参数校验
        $validate = Validate::rule([
            'order_no' => ValidateRule::isRequire(null, '缺少参数'),
            'send_id' => ValidateRule::isRequire(null, '缺少参数'),
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        //逻辑处理
        return BoxModel::OrderScan($input);
    }

    /**楼栋管理员--带取订单
     * @param Request $request
     * @return Json
     */
    public function DormitoryOrder(Request $request)
    {
        $input['manag_id'] = $request->post('manag_id');//楼栋管理员ID
        // 参数校验
        $validate = Validate::rule([
            'manag_id' => ValidateRule::isRequire(null, '缺少参数'),
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        //逻辑处理
        return BoxModel::DormitoryOrder($input);
    }

    /**楼栋管理员
     * @param Request $request
     * @return Json
     */
    public function report(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $type = $request->param('type');//1:装箱员 2 ：楼栋管理员
        $type_id = $request->param('type_id');//1:装箱员ID 2 ：楼栋管理员ID
        $start_time = $request->param('start_time');//开始时间
        $end_time = $request->param('end_time');//结束时间
        if (empty($type) || empty($type_id) || empty($start_time) || empty($end_time)) return jsonFail('缺少参数');
        if ($type == 1) {
            $fied = 'Boxing_id';
            $fied_status = 'box_status';
            $money = 'box_price';
        } else if ($type == 2) {
            $fied = 'manag_id';
            $fied_status = 'dor_manager_status';
            $money = 'dor_manager_price';
        } else {
            return jsonFail('格式错误');
        }
        $data['financial_money_1'] = Db::table('eb_order')->where($fied, $type_id)->where('is_pay', 1)->where('status', '>=', 4)->where($fied_status, 1)->sum($money);
        $data['financial_money_0'] = Db::table('eb_order')->where($fied, $type_id)->where('is_pay', 1)->where('status', '>=', 4)->where($fied_status, 0)->sum($money);
        $data['financial_num_1'] = Db::table('eb_order')->where($fied, $type_id)->where('is_pay', 1)->where('status', '>=', 4)->where($fied_status, 1)->count();
        $data['financial_num_0'] = Db::table('eb_order')->where($fied, $type_id)->where('is_pay', 1)->where('status', '>=', 4)->where($fied_status, 0)->count();
        return jsonSuccess('获取成功', $data);
    }

    /** 楼栋管理员
     * @param Request $request
     * @return Json
     */
    public function report_list(Request $request)
    {
        $type = $request->param('type');//1:装箱员 2 ：楼栋管理员
        $type_id = $request->param('type_id');//1:装箱员ID 2 ：楼栋管理员ID
        $financial= $request->param('financial',0);//0:未结算 1：已结算
        $page = $request->param('page',1);
        $limit = $request->param('limit',10);
        if(empty($type)||empty($type_id)) return jsonFail('缺少参数');
        if ($type == 1) {
            $fied = 'Boxing_id';
            $fied_status = 'box_status';
        } else if ($type == 2) {
            $fied = 'manag_id';
            $fied_status = 'dor_manager_status';
        } else {
            return jsonFail('格式错误');
        }
        $data = Db::table('eb_order')->page($page,$limit)->where($fied, $type_id)->where('is_pay', 1)->where('status', '>=', 4)->where($fied_status, $financial)->field('order_no,status,create_time')->select()->toArray();
        return jsonSuccess('获取成功', $data);
    }
}